/**
 * 
 */
package vista.domain;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JComponent;

import vista.ventanas.PanelPizarra;

import domain.Pizarra;

/**
 * @author De La
 *
 */
public class PizarraVisual extends JComponent implements Observer {
	
	private Pizarra pizarra;
	
	public PizarraVisual(Pizarra pizarra) {
		setPizarra(pizarra);
	}

	/* (non-Javadoc)
	 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
	 */
	public void update(Observable arg0, Object arg1) {
		paint( PanelPizarra.getInstance().getGraphics(), (Rectangle) arg1 );
	}
	
	private void paint(Graphics g, Rectangle r) {
		int minY = new Double(r.getMinY()).intValue();
		int minX = new Double(r.getMinX()).intValue();
		if (minY < 0) minY = 0;
		if (minX < 0) minX = 0;
		for (int y=minY; y<r.getMaxY(); y++) {
			for (int x=minX; x<r.getMaxX(); x++) {
				if ( (getPizarra().getMatriz()[x][y] != Pizarra.PUNTO_VACIO)  &&
						(getPizarra().getMatriz()[x][y] != Pizarra.PUNTO_PAPA) ) {
					g.setColor( new Color(0,0,0) ); //TODO Color de cada trazo
					g.fillRect(x, y, 1, 1);
				}
			}
		}
	}

	@Override
	public void paint(Graphics g) {
		for (int y=0;y<Pizarra.ALTO;y++) {
			for (int x=0;x<Pizarra.ANCHO;x++) {
				if ( getPizarra().getMatriz()[x][y] != Pizarra.PUNTO_VACIO ) {
					g.setColor( new Color(0,0,0) );
					g.fillRect(x, y, 1, 1);
				}
			}
		}
	}

	/**
	 * @return the pizarra
	 */
	public Pizarra getPizarra() {
		return pizarra;
	}

	/**
	 * @param pizarra the pizarra to set
	 */
	public void setPizarra(Pizarra pizarra) {
		this.pizarra = pizarra;
	}
}
